﻿using Microsoft.Extensions.DependencyInjection;
using PmSoft;

namespace Modules.System.Area
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AreaService
    {
        private readonly AreaRepository areaRepository;

        public AreaService(AreaRepository areaRepository)
        {
            this.areaRepository = areaRepository;
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SysArea? GetArea(int id)
        {
            return GetAllAreas().FirstOrDefault(m => m.Id == id);
        }

        public SysArea? Get(int id)
        {
            return areaRepository.Get(id);
        }

        /// <summary>
        /// 获取归属地完整拼接名称
        /// </summary>
        /// <param name="provinceCode"></param>
        /// <param name="cityCode"></param>
        /// <param name="areaCode"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetAreaFullName(int? provinceCode, int? cityCode, int? areaCode, string defaultValue = "")
        {
            var areas = GetAllAreas();
            string fullName = string.Empty;
            SysArea? sysArea = null;
            if (provinceCode.HasValue)
            {
                sysArea = areas.FirstOrDefault(m => m.Id == provinceCode);
                if (sysArea != null)
                    fullName += sysArea.Name;
            }
            if (cityCode.HasValue)
            {
                sysArea = areas.FirstOrDefault(m => m.Id == cityCode);
                if (sysArea != null)
                    fullName += "-" + sysArea.Name;
            }
            if (areaCode.HasValue)
            {
                sysArea = areas.FirstOrDefault(m => m.Id == areaCode);
                if (sysArea != null)
                    fullName += "-" + sysArea.Name;
            }
            if (string.IsNullOrEmpty(fullName))
                return defaultValue;
            return fullName;
        }

        /// <summary>
        /// 根据任意层级区划代码获取完整上级结构
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public (int? ProvinceCode, int? CityCode, int? AreaCode) GetAreaTuple(int code)
        {
            int? provinceCode = null, cityCode = null, areaCode = null;

            var area = GetArea(code);
            if (area == null)
                return (provinceCode, cityCode, areaCode);

            if (area.Deep == 0)
                provinceCode = area.Id;
            if (area.Deep == 1)
                cityCode = area.Id;
            if (area.Deep == 2)
                areaCode = area.Id;

            area = GetArea(area.Pid);
            if (area == null)
                return (provinceCode, cityCode, areaCode);

            if (area.Deep == 0)
                provinceCode = area.Id;
            if (area.Deep == 1)
                cityCode = area.Id;
            if (area.Deep == 2)
                areaCode = area.Id;
            area = GetArea(area.Pid);

            if (area == null)
                return (provinceCode, cityCode, areaCode);

            if (area.Deep == 0)
                provinceCode = area.Id;
            if (area.Deep == 1)
                cityCode = area.Id;
            if (area.Deep == 2)
                areaCode = area.Id;

            return (provinceCode, cityCode, areaCode);
        }

        /// <summary>
        /// 获取全部地区数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SysArea> GetAllAreas()
        {
            return areaRepository.GetAreas();
        }
    }
}